
package com.catsic.zcjc.web.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.catsic.base.BaseInfo;
import com.catsic.base.annotation.Log;
import com.catsic.base.controller.BaseController;
import com.catsic.base.tree.TreeInfo;
import com.catsic.core.entity.Organ;
import com.catsic.core.organ.service.OrganService;
import com.catsic.pub.drawer.service.DrawerService;
import com.catsic.pub.entity.Drawer;
import com.catsic.pub.form.DrawerForm;
import com.catsic.zcjc.condition.StationCondition;
import com.catsic.zcjc.constants.ZcjcConstants;
import com.catsic.zcjc.entity.District;
import com.catsic.zcjc.form.ListForm;
import com.catsic.zcjc.form.StationCountForm;
import com.catsic.zcjc.form.StationForm;
import com.catsic.zcjc.service.DistrictService;
import com.catsic.zcjc.service.StationService;


@Controller
@RequestMapping("/station")
public class StationController extends BaseController{
	
	@Autowired
	private StationService stationService;
	
	@Autowired
	private OrganService organService;
	
	@Autowired
	private DrawerService drawerService;
	
	@Autowired
	private DistrictService districtService;

	
    /**
     * 查询站点
	 * @author xietiebing
	 * @param condition 
	 * @return
	 */
	@RequestMapping(value = "/query")
	@ResponseBody
	public Map<String,Object> query(StationCondition condition,Pageable  pageable){
		if(condition.getCityId()!=null){
			List<District> dlist = districtService.findByCity(condition.getCityId());
			List<Long> areaIdList = new ArrayList<Long>();
			
			if(CollectionUtils.isNotEmpty(dlist)){
				for(District d : dlist){
					areaIdList.add(d.getId());
				}
				
				condition.setAreaIdList(areaIdList);
			}
		}
		Page<StationForm> page = stationService.findAll(condition, pageable);
		List<StationForm> list = page.getContent();
		if(CollectionUtils.isNotEmpty(list)){
			for(StationForm form : list){
				stationService.findFormatDrawer(form);
			}
		}
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, page);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * create:创建
	 * @author xietiebing
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/create" , method = RequestMethod.POST)
	@ResponseBody
	@Log(module = "站点管理", operation = "添加站点")
	public Map<String,Object> create(StationForm form){
		form.setCreateId(getCurrentUser().getUserId());
		form.setCreateName(getCurrentUser().getUsername());
		
		form = stationService.add(form);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * update:更新
	 * @author xietiebing
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/update" , method = RequestMethod.POST)
	@ResponseBody
	@Log(module = "站点管理", operation = "修改站点")
	public Map<String,Object> update(StationForm form){
		form = stationService.update(form);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	
	/**
	 * detail:详情
	 * @author xietiebing
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/detail" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> detail(String id){
		StationForm form =stationService.findStationById(id);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * delete:删除
	 * @author xietiebing
	 * @param id
	 * @return 
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	@Log(module = "站点管理", operation = "删除站点")
	public Map<String,Object> delete(String id){
		stationService.delete(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * typeList:站点类型列表
	 * @author xietiebing
	 * @param 
	 * @return
	 */
	@RequestMapping(value = "/typeList" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> typeList(){
		List<DrawerForm> drawerList = drawerService.findByParentCode(ZcjcConstants.DRAWER_STATION_TYPE);
		
		List<ListForm> list = new ArrayList<ListForm>();
		for(DrawerForm drawer : drawerList){
			ListForm form = new ListForm();
			form.setId(drawer.getValue());
			form.setText(drawer.getName());
			
			list.add(form);
		}
		
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, list);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * typeSonList:站点子类型列表
	 * @author xietiebing
	 * @param 
	 * @return
	 */
	@RequestMapping(value = "/typeSonList" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> typeSonList(String typeId){
		Drawer typeD = drawerService.findByParentAndValue(ZcjcConstants.DRAWER_STATION_TYPE, typeId);
		List<Drawer> drawerList = typeD.getChilds();
		
		List<ListForm> list = new ArrayList<ListForm>();
		for(Drawer drawer : drawerList){
			ListForm form = new ListForm();
			form.setId(drawer.getValue());
			form.setText(drawer.getName());
			
			list.add(form);
		}
		
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, list);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * tree:站点树
	 * @author xietiebing
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/tree")
	@ResponseBody
	public Map<String,Object> tree(String id){
		List<TreeInfo> form = stationService.getTreeInfo(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT,form);
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * countByArea:根据行政区域统计
	 * @author xietiebing
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/countByArea")
	@ResponseBody
	public Map<String,Object> countByArea(String type,Integer areaId){
		List<StationCountForm> form = stationService.findCountByArea(type,areaId);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT,form);
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * countByArea:根据机构和行政区域统计
	 * @author xietiebing
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/countByAreaAndOrg")
	@ResponseBody
	public Map<String,Object> countByAreaAndOrg(){
		BaseInfo baseInfo = getCurrentUser();
		Organ org = organService.findByCode(baseInfo.getOrganCode());
		
		List<StationCountForm> form = stationService.findCountByAreaAndOrg(org,null);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("org_level",org.getJb());
		map.put(CONTENT,form);
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
}
